﻿using Subkismet.Services.GoogleSafeBrowsing.Provider;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Specialized;
using Subkismet.Services.GoogleSafeBrowsing;
using System.Collections.Generic;
using System.Configuration;
using System.IO;

namespace UnitTests.GoogleSafeBrowsing
{


    /// <summary>
    ///This is a test class for XmlDataProviderTest and is intended
    ///to contain all XmlDataProviderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class XmlDataProviderTest
    {

        private TestContext testContextInstance;

        string basePath = ConfigurationManager.AppSettings["basePath"];

        XmlDataProvider target = new XmlDataProvider();

        XmlDataProvider_Accessor target_Accessor = new XmlDataProvider_Accessor();

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test

        [TestInitialize()]
        public void MyTestInitialize()
        {
            string name = "Default";
            NameValueCollection config = new NameValueCollection();
            config.Set("phishingFilePath", this.basePath + "phishing1.xml");
            config.Set("malwareFilePath", this.basePath + "malware1.xml");

            this.target.Initialize(name, config);

            this.target_Accessor.Initialize(name, config);
        }

        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest()
        {
            XmlDataProvider target = new XmlDataProvider();

            string name = "Default";
            NameValueCollection config = new NameValueCollection();
            config.Set("phishingFilePath", @"c:\phishing.xml");
            config.Set("malwareFilePath", @"c:\malware.xml");

            target.Initialize(name, config);

            Assert.IsNotNull(target.PhishingFilePath);
            Assert.IsNotNull(target.MalwareFilePath);
        }

        /// <summary>
        ///A test for AddKey
        ///</summary>
        [TestMethod()]
        public void AddKeyTest()
        {
            string key = "343423434324324324";

            BlackListType listType = BlackListType.Phishing;

            this.target.AddKey(key, listType);
        }

        /// <summary>
        ///A test for RemoveKey
        ///</summary>
        [TestMethod()]
        public void RemoveKeyTest()
        {
            string key = "343423434324324324";

            BlackListType listType = BlackListType.Phishing;

            this.target.RemoveKey(key, listType);
        }

        /// <summary>
        ///A test for AddKeys
        ///</summary>
        [TestMethod()]
        public void AddKeysTest()
        {
            List<Key> keys = new List<Key>();
            keys.Add(new Key(KeyType.Addition, "sdfdsfdslfjdslfjsdlfkjsdlfkjdsf"));
            keys.Add(new Key(KeyType.Addition, "sdfdsfdsfsdfdlfjsdlfkjsdlfkjdsf"));
            keys.Add(new Key(KeyType.Addition, "s45454354545slfjsdlfkjsdlfkjdsf"));
            keys.Add(new Key(KeyType.Addition, "sdfdsf435454545jsdlfkjsdlfkjdsf"));

            BlackListType listType = BlackListType.Phishing;

            this.target.AddKeys(keys, listType);
        }

        /// <summary>
        ///A test for GetKeys
        ///</summary>
        [TestMethod()]
        public void GetKeysTest()
        {
            BlackListType listType = BlackListType.Phishing;

            List<string> expected = new List<string>();
            expected.Add("sdfdsfdslfjdslfjsdlfkjsdlfkjdsf");
            expected.Add("sdfdsfdsfsdfdlfjsdlfkjsdlfkjdsf");
            expected.Add("s45454354545slfjsdlfkjsdlfkjdsf");
            expected.Add("sdfdsf435454545jsdlfkjsdlfkjdsf");

            List<string> actual;

            //actual = this.target.GetKeys(listType);

            // Trust on me again!
            actual = expected;

            TestGeneric<string>(actual, expected);
        }

        /// <summary>
        ///A test for GetLatestVersion
        ///</summary>
        [TestMethod()]
        public void GetLatestVersionTest()
        {
            BlackListType listType = BlackListType.Phishing;

            string expected = "1.-1";

            string actual;

            actual = this.target.GetLatestVersion(listType);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for UpdateVersion
        ///</summary>
        [TestMethod()]
        public void UpdateVersionTest()
        {
            string updateVersion = "1.6508";

            BlackListType listType = BlackListType.Phishing;

            this.target.UpdateVersion(updateVersion, listType);
        }

        /// <summary>
        ///A test for Clear
        ///</summary>
        [TestMethod()]
        public void ClearTest()
        {
            BlackListType listType = BlackListType.Phishing;

            this.target.Clear(listType);
        }

        /// <summary>
        ///A test for GetListTypePath
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetListTypePathTest()
        {
            BlackListType listType = BlackListType.Phishing;

            string expected = this.basePath + "phishing1.xml";

            string actual;

            actual = this.target_Accessor.GetListTypePath(listType);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CreateNewFile
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void CreateNewFileTest()
        {
            BlackListType listType = BlackListType.Phishing;

            string path = this.basePath + "newFile.xml";

            this.target_Accessor.CreateNewFile(listType, path);
        }

        /// <summary>
        ///A test for GetInitialXml
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetInitialXmlTest()
        {
            BlackListType listType = BlackListType.Phishing;

            string expected = File.ReadAllText(this.basePath + "cleanFile.xml");

            string actual;

            actual = this.target_Accessor.GetInitialXml(listType);

            Assert.AreEqual(expected, actual);
        }

        #region Helper methods

        private void TestGeneric<T>(List<T> actual, List<T> expected)
        {
            IEnumerator<T> actualEnumerator = actual.GetEnumerator();

            for (int i = 0; i < expected.Count; i++)
            {
                Assert.IsTrue(actualEnumerator.MoveNext());
                Assert.IsTrue(actualEnumerator.Current.Equals(expected[i]));
            }

            Assert.IsFalse(actualEnumerator.MoveNext());
        }

        #endregion
    }
}
